<?php
App::uses('AppController', 'Controller');
/**
 * Categories Controller
 *
 * @property Category $Category
 */
class CategoriesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Category->recursive = 0;
		$this->set('categories', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		$this->Category->id = $id;
		if (!$this->Category->exists()) {
			throw new NotFoundException(__('Invalid category'));
		}
		$this->set('category', $this->Category->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		$this->loadModel('Parameter');
		if ($this->request->is('post')) {
			$this->Category->create();
			if(!empty($this->request->data['Category']['parent_id'])){
				$parent = $this->Category->findById($this->request->data['Category']['parent_id']);
				$this->request->data['Category']['level'] = $parent['Category']['level'] + 1;
			}			

			$this->request->data['Category']['created_at'] = 	date("Y-m-d H:i:s");
			$this->request->data['Category']['updated_at'] = 	date("Y-m-d H:i:s");
			$this->request->data['Category']['created_by'] = $this->Auth->user('id');


			if ($this->Category->save($this->request->data)) {
				$this->Session->setFlash(__('The category has been saved'), 'default', array(), 'good');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'), 'default', array(), 'bad');
			}
		}
		$parents = $this->Category->generateTreeList(null, null, null,  "&raquo;&rarr;&nbsp;&nbsp;");
		$types = $this->Parameter->getCategoryTypes();
		$statuses = $this->Parameter->getCategoryStatus();
		
		$this->set(compact('statuses'));
		$this->set(compact('types'));
		$this->set(compact('parents'));
	}


/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		$this->loadModel('Parameter');
		$this->Category->id = $id;
		if (!$this->Category->exists()) {
			throw new NotFoundException(__('Invalid category'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Category->save($this->request->data)) {
				$this->Session->setFlash(__('The category has been saved'), 'default', array(), 'good');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'), 'default', array(), 'bad');
			}
		} else {
			$this->request->data = $this->Category->read(null, $id);
		}
		#$parents = $this->Category->Parent->find('list');
		$types = $this->Parameter->getCategoryTypes();
		$parents = $this->Category->generateTreeList(null, null, null,  "&nbsp;&nbsp;&rsaquo;&rarr;&nbsp;&nbsp;");
		$statuses = $this->Parameter->getCategoryStatus();

		$this->set(compact('parents'));
		$this->set(compact('statuses'));
		$this->set(compact('types'));
	}


/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Category->id = $id;
		if (!$this->Category->exists()) {
			throw new NotFoundException(__('Invalid category'));
		}
		if ($this->Category->delete()) {
			$this->Session->setFlash(__('Category deleted'), 'default', array(), 'good');
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Category was not deleted'), 'default', array(), 'bad');
		$this->redirect(array('action' => 'index'));
	}
}
